using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface IContractService : IBaseService<Contract> {
        ContractItem GetNext();
        ContractItem GetItem(int conid);
        IList<ContractItem> GetItemList(Func<ContractItem, bool> filters);
        void Update(ContractItem model);
    }

    public class ContractService : BaseService, IContractService {
        public IList<Contract> GetList(Func<Contract, bool> filters) {
            if (filters != null)
                return db.Contracts.Where(filters).ToList();
            else
                return db.Contracts.ToList();
        }

        public Contract Find(params object[] keys) {
            return db.Contracts.Find(keys);
        }

        public void Add(Contract model) {
            db.Contracts.Add(model);
        }

        public void Update(Contract model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Contract model) {
            db.Contracts.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public ContractItem GetNext() {
            int id = 1;
            if (db.Contracts.Count() > 0)
                id = db.Contracts.Max(c => c.Id) + 1;
            string manualno = "HT" + DateTime.Now.ToString("yyyyMMdd") + id.ToString();
            var contract = new Contract { Id = id, Manualno = manualno, Startime = DateTime.Now, Endtime = DateTime.Now };
            return new ContractItem { Contract = contract };
        }

        public IList<ContractItem> GetItemList(Func<ContractItem, bool> filters) {
            var query = from ct in db.Contracts
                        from cu in db.Customers
                        where ct.Customerid == cu.CustomerId
                        select new ContractItem {
                            Customer = cu,
                            Contract = ct,
                        };
            if (filters != null) {
                return query.Where(filters).ToList();
            } else {
                return query.ToList();
            }
        }

        public ContractItem GetItem(int conid) {
            var query = from ct in db.Contracts
                        join cu in db.Customers on ct.Customerid equals cu.CustomerId
                        join cd in db.ContractRates on ct.Id equals cd.Conid into gp
                        where ct.Id == conid
                        //from item in gp.DefaultIfEmpty()
                        select new ContractItem {
                            Customer = cu,
                            Contract = ct,
                            ContractRate = gp
                        };
            return query.FirstOrDefault();
        }


        public void Update(ContractItem model) {
            Update(model.Contract);
            var dcTime = model.Contract.Modifytime;
            var user = model.Contract.Modifyuser;
            foreach (var detail in model.ContractRate) {
                detail.Modifyuser = user;
                detail.Modifytime = dcTime;
                db.Entry(detail).State = EntityState.Modified;
            }
        }
    }
}
